package com.bwie.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.domain.Steam;
import com.bwie.mapper.SteamMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Li sir
 * @Date: 2024/10/30 08:26
 * @Description:
 **/
@Service
public class SteamServiceImpl implements SteamService {

        @Autowired
    private SteamMapper steamMapper;


    @Override
    public R list(PageInfoVo pageInfoVo) {
        Page<Steam> steamPage = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        LambdaQueryWrapper<Steam> queryWrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(pageInfoVo.getKeyWord())){
            queryWrapper.like(Steam::getName,pageInfoVo.getKeyWord());
        }
        if (!StringUtils.isEmpty(pageInfoVo.getType())){
            queryWrapper.eq(Steam::getType,pageInfoVo.getType());
        }
        Page<Steam> steamPage1 = steamMapper.selectPage(steamPage, queryWrapper);

        return  R.OK(steamPage1);


        //模糊查询


    }

    @Override
    public R add(Steam steam) {
        LambdaQueryWrapper<Steam> steamLambdaQueryWrapper = new LambdaQueryWrapper<>();
        steamLambdaQueryWrapper.eq(Steam::getName,steam.getName());
        steamMapper.selectOne(steamLambdaQueryWrapper);
        if (steamMapper.selectOne(steamLambdaQueryWrapper) != null){
            return R.ERROR(400,"商品已存在");
        }

        steamMapper.insert(steam);

        return R.OK();
    }
}
